package com.service.atf.base.model;

import com.service.atf.base.type.BuffReaderHead;
import com.service.atf.base.utils.ATFConstants;
import com.service.atf.base.utils.ATFLogger;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.Date;

/**
 * User: luotao
 * Date: 12-2-25
 * Time: 13:14
 * /**
 * ATFCODE	NUMBER(8)	N			ATF������־����
 * ATFSTATUS	NUMBER(1)	N			ATF������־״̬��0=ʹ���С�1=ʹ���꣩
 * ATFPREVCODE	NUMBER(8)	N			ATF������־ǰһ������
 * ATFNEXTCODE	NUMBER(8)	Y			ATF������־��һ������
 * ATFCREATDATE	DATE	N			ATF������־����ʱ��
 * ATFFILEPATH	VARCHAR2(50)	N			ATF������־�ļ�·��
 */

public class TFInfo {


    private int atfCode;

    private String atfStatus;

    private int atfPrevCode;

    private int atfNextCode;

    private Date atfCreateDate;

    private String atfFilePath;

    private int checkPointIndex = 5;


    public TFInfo parseByDB() {

//        atfCode = qResults.currentInt("ATFCODE");
//        atfStatus = qResults.current("ATFSTATUS");
//        atfPrevCode = qResults.currentInt("ATFPREVCODE");
//        atfNextCode = qResults.currentInt("ATFNEXTCODE");
//        atfCreateDate = qResults.currentDate("ATFCREATDATE");
//        atfFilePath = qResults.current("ATFFILEPATH");
        return this;
    }


    //�������һ��checkpoint���ƫ����
    public int findLastCheckPoint() {
        try {
            RandomAccessFile file = new RandomAccessFile(atfFilePath, "rw");
            FileChannel channel = file.getChannel();
            BuffReaderHead read = new BuffReaderHead(channel, checkPointIndex);
            int position = read.read();
            channel.close();
            file.close();
            return position;
        } catch (FileNotFoundException e) {
            String msg = "����checkpointʱû���ҵ�Ŀ���ļ�,path : " + atfFilePath;
            ATFLogger.syserr(msg);
            throw new RuntimeException(msg);
        } catch (IOException e) {
            String msg = "����TFʱ�ر��ļ�ʱ�������,path : " + atfFilePath;
            ATFLogger.syserr(msg);
            throw new RuntimeException(msg);
        }
    }

    public int getAtfCode() {
        return atfCode;
    }

    public void setAtfCode(int atfCode) {
        this.atfCode = atfCode;
    }

    public String getAtfStatus() {
        return atfStatus;
    }

    public void setAtfStatus(String atfStatus) {
        this.atfStatus = atfStatus;
    }

    public int getAtfPrevCode() {
        return atfPrevCode;
    }

    public void setAtfPrevCode(int atfPrevCode) {
        this.atfPrevCode = atfPrevCode;
    }

    public int getAtfNextCode() {
        return atfNextCode;
    }

    public void setAtfNextCode(int atfNextCode) {
        this.atfNextCode = atfNextCode;
    }

    public Date getAtfCreateDate() {
        return atfCreateDate;
    }

    public void setAtfCreateDate(Date atfCreateDate) {
        this.atfCreateDate = atfCreateDate;
    }

    public String getAtfFilePath() {
        return atfFilePath;
    }

    public void setAtfFilePath(String atfFilePath) {
        this.atfFilePath = atfFilePath;
    }

    public int getCheckPointIndex() {
        return checkPointIndex;
    }

    public void setCheckPointIndex(int checkPointIndex) {
        this.checkPointIndex = checkPointIndex;
    }

    public TFInfo createTFInfo(TFInfo prevInfo, int thisId) {
        //todo
        this.atfPrevCode = prevInfo.getAtfCode();
        this.atfCode = thisId;
        this.atfStatus = "0";
        this.atfCreateDate = new Date();
        StringBuffer buffer = new StringBuffer(ATFConstants.TF_PATH_PRIFIX);
        buffer.append(atfCode).append(ATFConstants.TF_PATH_SUFFIX);
        this.atfFilePath = buffer.toString();
        return this;
    }

    public TFInfo createFirstTF(int id) {
        setAtfCode(id);
        setAtfCreateDate(new Date());
        String s_now_date = "";
        s_now_date = s_now_date.substring(2);
        String tf_path = ATFConstants.TF_PATH_PRIFIX + s_now_date + "0001" + ATFConstants.TF_PATH_SUFFIX;
        ATFLogger.sysinfo("�½�TF�ļ� : " + tf_path);
        setAtfFilePath(tf_path);
        setAtfNextCode(0);
        setAtfPrevCode(0);
        setAtfStatus("0");
        return this;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(File.separator);
        TFInfo tfInfo = new TFInfo();
    }
}
